/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.EleSourceMultimedia;
import com.thinkgem.jeesite.modules.source.entity.image.EleSourceImageGroup;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideoGroup;
import com.thinkgem.jeesite.modules.source.service.image.EleSourceImageGroupService;
import com.thinkgem.jeesite.modules.source.service.video.EleSourceVideoGroupService;
import com.thinkgem.jeesite.modules.source.dao.EleSourceMultimediaDao;

/**
 * 多媒体资源Service
 * @author unnamed
 * @version 2017-07-10
 */
@Service
@Transactional(readOnly = true)
public class EleSourceMultimediaService extends CrudService<EleSourceMultimediaDao, EleSourceMultimedia> {
	@Autowired
	private EleSourceImageGroupService eleSourceImageGroupService;
	@Autowired
	private EleSourceVideoGroupService eleSourceVideoGroupService;
	
	public EleSourceMultimedia get(String id) {
		return super.get(id);
	}
	
	public List<EleSourceMultimedia> findList(EleSourceMultimedia eleSourceMultimedia) {
		return super.findList(eleSourceMultimedia);
	}
	
	public Page<EleSourceMultimedia> findPage(Page<EleSourceMultimedia> page, EleSourceMultimedia eleSourceMultimedia) {
		return super.findPage(page, eleSourceMultimedia);
	}
	
	@Transactional(readOnly = false)
	public void save(EleSourceMultimedia eleSourceMultimedia) {
		EleSourceVideoGroup videoGroup = eleSourceMultimedia.getVideoGroup();
		EleSourceImageGroup imageGroup = eleSourceMultimedia.getImageGroup();
		long current = System.currentTimeMillis();
		if(videoGroup != null){
			videoGroup.setName("video-"+current);
			eleSourceVideoGroupService.save(videoGroup);
			eleSourceMultimedia.setVideoId(videoGroup.getId());
			eleSourceMultimedia.setImageId(null);
		}
		if(imageGroup != null)
		{
			imageGroup.setName("image-"+current);

			eleSourceImageGroupService.save(imageGroup);
			eleSourceMultimedia.setImageId(imageGroup.getId());
			eleSourceMultimedia.setVideoId(null);
		}
		super.save(eleSourceMultimedia);
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceMultimedia eleSourceMultimedia) {
		super.delete(eleSourceMultimedia);
	}
	
}